package org.linys.service.sale;

import java.util.List;

import org.linys.dao.sale.SaleDAO;
import org.linys.dao.sale.SaleDetailDAO;
import org.linys.dao.sale.SaleFeeDAO;
import org.linys.model.sale.Sale;
import org.linys.model.sale.SaleDetail;
import org.linys.model.sale.SaleFee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SaleServ {
	private SaleDAO saleDAO;
	private SaleDetailDAO saleDetailDAO;
	private SaleFeeDAO saleFeeDAO;
	

	public SaleDAO getSaleDAO() {
		return saleDAO;
	}
	@Autowired
	public void setSaleDAO(SaleDAO saleDAO) {
		this.saleDAO = saleDAO;
	}
	public int save(Sale sale) {
		if (saleDAO.save(sale) > 0) {
			List<SaleDetail> list = sale.getSaleDetailList();
			for (SaleDetail saleDetail : list) {
				saleDetail.setHandlerID(sale.getHandlerID());
				saleDetail.setSaleID(sale.getSaleID());
				saleDetailDAO.save(saleDetail);
			}
			List<SaleFee> feeList = sale.getSaleFeeList();
			for (SaleFee saleFee : feeList) {
				saleFee.setHandlerID(sale.getHandlerID());
				saleFee.setSaleID(sale.getSaleID());
				saleFeeDAO.save(saleFee);
			}
			return 1;
		} else {
			return 0;
		}
	}
	
	public int update(Sale sale) {
		try{
			saleDAO.update(sale);
			List<SaleDetail> list = sale.getSaleDetailList();
			for (SaleDetail saleDetail : list) {
				saleDetail.setOperateTime(sale.getOperateTime());
				saleDetailDAO.saveTemp(saleDetail);
			}
			//调用存储过程将变化的货物信息保存到真实表中
			sale.setFlag("UpdateSave");
			saleDAO.editP(sale);
			
			saleFeeDAO.deleteBySaleID(sale.getSaleID());
			List<SaleFee> feeList = sale.getSaleFeeList();
			for (SaleFee saleFee : feeList) {
				saleFee.setOperateTime(sale.getOperateTime());
				saleFeeDAO.update(saleFee);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return 1;
	}
	
	public SaleDetailDAO getSaleDetailDAO() {
		return saleDetailDAO;
	}
	@Autowired
	public void setSaleDetailDAO(SaleDetailDAO saleDetailDAO) {
		this.saleDetailDAO = saleDetailDAO;
	}
	public SaleFeeDAO getSaleFeeDAO() {
		return saleFeeDAO;
	}
	@Autowired
	public void setSaleFeeDAO(SaleFeeDAO saleFeeDAO) {
		this.saleFeeDAO = saleFeeDAO;
	}
	public List<Sale> view(Sale sale, Integer start, Integer rows) {
		return saleDAO.view(sale,start,rows);
	}
	public int count(Sale sale) {
		return saleDAO.count(sale);
	}
	public int deleteByID(Integer id) {
		 return saleDAO.deleteByID(id);
	}
	public Sale getBySaleID(Integer saleID) {
		return saleDAO.getBySaleID(saleID);
	}
	public int updateStatus(Sale sale) {
		return saleDAO.updateStatus(sale);
		
	}
	public int updateStatusByIDs(Sale sale) {
		String[] idsStr = sale.getIds().split(",");
		for (String id : idsStr) {
			try {
				sale.setSaleID(new Integer(id));
				saleDAO.updateStatus(sale);
			} catch (Exception e) {
				e.printStackTrace();
				return 0;
			}
		}
		return 1;
	}
	public int validateCode(Sale sale) {
		return saleDAO.validateCode(sale);
	}
	

}
